var zNodes='';
var companyId ="{$Think.get.company_id}"
//获取单位性质树状
var setting = {
    check: {
        enable: true,
        chkDisabledInherit: true
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};

if(companyId){

}else{
     ajax(api.config.data,{},'post').done(function(res){
    console.log(res.data.use_prop)
    if(res.code == 1 ){
        zNodes =res.data.use_prop;
       for(var key in zNodes){
           zNodes[key].open = true;
       }
        $.fn.zTree.init($("#ztreeid"), setting, zNodes);
    }else{
        Err('数据获取失败')
    }
}).fail(function(err){
    Err('请求失败')
})
}
 
// 循环遍历

{if $Think.get.company_id}
	ajax(api.company.list,{
		where_json:JSON.stringify({
			company_id:['in',"{$Think.get.company_id}"]
		})
	},'post').done(function(res) {
		
		console.log('编辑')
		console.log(res)
		for(var key in res.data[0]){
            if(res.data[0][key]){
            $('input[name='+key+']').val(res.data[0][key])                
            }else{
            $('input[name='+key+']').attr('placeholder','暂无信息')                                 
            }

            $('select[name=company_mode]').val(res.data[0]['company_mode'])

            if(key == 'remark'){
                if(res.data[0]['remark']){
                    $('textarea[name=remark]').text(res.data[0]['remark'])
                }else{
                    $('textarea[name=remark]').attr('placeholder','暂无备注')    
                }    
            }
           if(key == 'license'){
            $('#img1').attr('src',domain+res.data[0]['license'])            
           }else{
            $('#img1').attr('src','/assets/img/noimg.gif')                           
           }

           if(key =='prop'){
            var treeii =res.data[0][key].split(',');
            ajax(api.config.data,{},'post').done(function(res){
                console.log(res.data.use_prop)
                console.log(treeii)
                if(res.code == 1 ){
                    zNodes = res.data.use_prop;
                    for(var i =0;i<zNodes.length;i++){
                        for(var key in zNodes){
                       zNodes[key].open = true;
                       if(zNodes[key].id == treeii[i]){
                        zNodes[key].checked = true;
                       }
                   }
                }                  
                    $.fn.zTree.init($("#ztreeid"), setting, zNodes);
                }else{
                    Err('数据获取失败')
                }
                Load(1)
            }).fail(function(err){
                Err('请求失败')
            })
           }
        }
        form.render('select');
	}).fail(function(err) {
		Err('请求失败')
	});
	{else/}
	Load(1)
{/if}
// 上传图片
var cp_data= {};
upImage(api.upimage.upimage,'#test1', '#img1',cp_data,'img');

//用户列表
$('.username').on('click',function(){
	opsearch('{:url("html/system/user_add")}?op=search&role_id={$setting.role_passenger}')
})
// 表单提交
form.on('submit(save)', function(data){
    var treeObj = $.fn.zTree.getZTreeObj("ztreeid");
	var nodes = treeObj.getNodes();
	var text ='';
	for(var i = 0; i < nodes.length; i++) {
		if(nodes[i].checked) {
			text+=String(nodes[i].id)
		}
		if(nodes[i].children) {
			for(var j = 0; j < nodes[i].children.length; j++) {
				if(nodes[i].children[j].checked) {
					text+=String(nodes[i].children[j].id)
				}
				if(nodes[i].children[j].children) {
					for(var z = 0; z < nodes[i].children[j].children.length; z++) {
						if(nodes[i].children[j].children[z].checked) {
							text+=String(nodes[i].children[j].children[z].id)
						}
					}
				}
			}
		}
    }

    var prop=String(text.split("")) ;
    Load()
    $('input[name="img"]').val(data.img)
	  console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
      console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
      console.log('上传信息')
      console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
      {if $Think.get.company_id}

       var company_id = {$Think.get.company_id};
        data.field.company_id = company_id;
        data.field.license = cp_data.img;
        data.field.prop = prop;
        console.log(data.field)
	 	ajax(api.company.edit,data.field,'post').done(function(res) {
                console.log(res)
                Load(1)
                if(res.code==1){
                    Msg('编辑成功')
                    window.location.href='{:url("/html/system/company")}'
                }else{
                    Err(res.msg)
                }
		}).fail(function(err) {
			Err('请求失败')
        });
        {else/}
        data.field.license = cp_data.img;
        data.field.prop = prop;
        console.log(data.field)
        ajax(api.company.add,data.field,'post').done(function(res) {
            console.log(res)
            Load(1)
            if(res.code==1){
                Msg('添加成功')
                 window.location.href='{:url("/html/system/company")}'
            }else{
                Err(res.msg)
            }
    }).fail(function(err) {
        Err('请求失败')
    });
    {/if}
	  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});

// 返回上一页
$('.back').on('click',()=>{
     window.location.href='{:url("/html/system/company")}'
})
